package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.HomeTabLayoutAdapter;
import com.shaoshaohuo.app.ui.fragment.MyOrderFragment;

import java.util.ArrayList;
import java.util.List;

public class ShopAllOrderActivity extends AppCompatActivity {
    /**
     * tobar
     */
    private ImageView order_tobar_back;
    private TextView order_tobar_enter;
    private ImageView order_tobar_right;
    private LinearLayout order_layout_topbar_left;

    private String intent_data = "";
    private int intent_position = 0;
    private TabLayout my_order_tabLayout;
    private ViewPager my_order_viewPager;
    private List<String> my_order_tab_list;
    private MyOrderFragment my_order_fragment;
    private List<Fragment> my_order_fragment_list;
    private HomeTabLayoutAdapter my_order_tabLayout_adapter;


    public ShopAllOrderActivity newInstance(Intent intent, String data, int position) {
        ShopAllOrderActivity mShopAllOrderActivity = new ShopAllOrderActivity();
        Bundle bundle = new Bundle();
        bundle.putString("intent_data", data);
        bundle.putInt("intent_position", position);
        intent.putExtras(bundle);
        return mShopAllOrderActivity;
    }

    private void getIntentData() {
        Intent intent = getIntent();
        if (intent != null) {
            intent_data = intent.getExtras().getString("intent_data");
            intent_position = intent.getExtras().getInt("intent_position");
            my_order_tabLayout.getTabAt(intent_position).select();
            my_order_viewPager.setCurrentItem(intent_position);
            if (intent_position > 0) {
                my_order_fragment.newInstance(intent_data, "");
            }
        }


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_all_order);
        initView();
        initData();
        bindAdapter();
        initListener();
    }

    private void initListener() {
        order_tobar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void bindAdapter() {
        my_order_viewPager.setAdapter(my_order_tabLayout_adapter);
        my_order_tabLayout.setupWithViewPager(my_order_viewPager);
        getIntentData();
    }

    private void initData() {
        my_order_tab_list.add("全部");
        my_order_tab_list.add("待付款");
        my_order_tab_list.add("待发货");
        my_order_tab_list.add("待确认");
        my_order_tab_list.add("已完成");
        my_order_tab_list.add("待退款");
        for (int i = 0; i < my_order_tab_list.size(); i++) {
            my_order_fragment = new MyOrderFragment();
            my_order_fragment_list.add(my_order_fragment.newInstance(my_order_tab_list.get(i), i + 1 + ""));
            my_order_tabLayout.addTab(my_order_tabLayout.newTab().setText(my_order_tab_list.get(i)));
        }
    }
    private void initView() {
        /**
         * tobar
         * */
        order_tobar_back = (ImageView) findViewById(R.id.imageview_topbar_left_backImage);
        order_tobar_back.setVisibility(View.VISIBLE);
        order_tobar_back.setImageResource(R.mipmap.return_key);
        order_tobar_enter = (TextView) findViewById(R.id.toolbar_topname);
        order_tobar_enter.setText("我的订单");
        order_tobar_right = (ImageView) findViewById(R.id.imageview_message);
        order_tobar_right.setVisibility(View.GONE);
        order_layout_topbar_left = (LinearLayout) findViewById(R.id.layout_topbar_left);
        order_layout_topbar_left.setVisibility(View.VISIBLE);
        /**
         * tablayout
         * */
        my_order_tabLayout = (TabLayout) findViewById(R.id.my_order_tabLayout);
        my_order_tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        my_order_viewPager = (ViewPager) findViewById(R.id.my_order_viewPager);
        my_order_tab_list = new ArrayList<>();
        my_order_fragment_list = new ArrayList<>();
        my_order_tabLayout_adapter = new HomeTabLayoutAdapter(getSupportFragmentManager(), this, my_order_tab_list, my_order_fragment_list);

    }
}
